import Vue from "vue";
import Router from "vue-router";
// 导入动态路由配置模块
import approvalsRouter from "./modules/approvals";
import departmentsRouter from "./modules/departments";
import employeesRouter from "./modules/employees";
import permissionsRouter from "./modules/permissions";
import attendancesRouter from "./modules/attendances";
import salarysRouter from "./modules/salarys";
import settingsRouter from "./modules/settings";
import socialsRouter from "./modules/socials";

Vue.use(Router);

/* Layout */
import Layout from "@/layout";

/**
 * vue-elmenent-template规定的路由配置规则
 * 注意: sub-menu只会出现在子级路由大于等于1个的情况 
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   如果设置为true在则永远都不会显示在侧边栏 if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               如果设置true，将作为一级菜单展示 if set true, will always show the root menu
 *                                如果没有设置，当前子级超过1个的时候显示，否则就是根级别菜单 if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           重定向地址 if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             路由的名称，如果设置组件缓存，则必须使用 the name is used by <keep-alive> (must set!!!)
 * meta : {                       路由元信息
    roles: ['admin','editor']     控制角色的 【我们项目中不需要，用别的方式实现】 control the page roles (you can set multiple roles)
    title: 'title'                菜单标题  the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x'  菜单图标 the icon show in the sidebar
    breadcrumb: false             该菜单是否显示路径导航中 if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'   是否一直高亮激活的  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * 静态路由 constantRoutes
 * 不需要权限，所有人都可以访问的路由
 */
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },
  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "Dashboard", icon: "dashboard" },
      },
    ],
  },
  {
    path: "/import",
    component: Layout,
    hidden: true, // 隐藏在左侧菜单中
    children: [
      {
        path: "", // 二级路由path什么都不写 表示二级默认路由
        component: () => import("@/views/import"),
      },
    ],
  },
  // 404 page must be placed at the end !!!
  // { path: "*", redirect: "/404", hidden: true },
];

/**
 * 动态路由
 * 需要权限的，不同角色的用户看到的是不一样的
 */
export const asyncRoutes = [
  approvalsRouter,
  departmentsRouter,
  employeesRouter,
  permissionsRouter,
  attendancesRouter,
  salarysRouter,
  settingsRouter,
  socialsRouter,
  // {}, // 考勤模块
  // {}, // 工资模块
  // {}, // 社保模块
  // // .... 很多很多，我们要把这些细致的配置进行抽离出去，让维护更加容易
];

// 原来  new VueRouter({配置})
// 定义一个创建路由的函数
const createRouter = () =>
  new Router({
    mode: "hash",
    // mode: "history", // require service support
    // base: "/hr", // 路由基准地址
    scrollBehavior: () => ({ y: 0 }),
    // routes: constantRoutes, // constantRoutes静态路由映射
    // routes: [...constantRoutes, ...asyncRoutes], // 静态+动态 放在一起
    routes: [...constantRoutes], // 只有静态  放在一起
  });
// 创建路由实例对象
const router = createRouter();

// 重置路由实例的方法
// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter(); // 重新创建一个路由实例
  router.matcher = newRouter.matcher; // 将新的路由实例的matcher属性赋给原来路由实例的matcher属性，即可实现原路由实例重置
}

export default router;
